package com.ruoyi.job.task;

import cn.hutool.system.SystemUtil;
import cn.hutool.system.oshi.OshiUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.vo.SystemInfoVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.querydsl.QSort;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;

@SpringBootTest
public class EsTest {

    @Autowired
    @Qualifier("elasticsearchClient")
    ElasticsearchClient client;

    @Autowired
    @Qualifier("elasticsearchTemplate")
    ElasticsearchTemplate elasticsearchTemplate;
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //测试创建索引
    @Test
    public void createIndex() throws IOException {
        SystemInfoVo systemInfoVo = new SystemInfoVo();
        systemInfoVo.setId(UUID.fastUUID().toString());

        // cpu
        systemInfoVo.setCpuSystemUsage(OshiUtil.getCpuInfo().getUsed());


        //  jvm总内存 4001,
        systemInfoVo.setMemoryJvmMax(SystemUtil.getRuntimeInfo().getMaxMemory());
        systemInfoVo.setMemoryJvmFree(SystemUtil.getRuntimeInfo().getFreeMemory());
        systemInfoVo.setMemoryJvmTotal(SystemUtil.getRuntimeInfo().getTotalMemory());

        //磁盘
        systemInfoVo.setDiskTotal(OshiUtil.getDiskStores().stream().mapToLong(s->s.getSize()).sum());
        systemInfoVo.setDiskFree(OshiUtil.getDiskStores().stream().mapToLong(s->s.getWriteBytes()).sum());
        //时间
        Date date = new Date();
        systemInfoVo.setTimestamp(date.getTime());
        systemInfoVo.setTimeString(simpleDateFormat.format(date));
        client.index(i-> i.index("system-info").id(systemInfoVo.getId())
                        .document(systemInfoVo));
    }
    @Test
    public void test1() throws IOException {
        SortOptions timestamp = SortOptions.of(s ->
                s.field(fn -> fn.field("timestamp").order(SortOrder.Desc))
        );
        SearchRequest searchRequest = new SearchRequest.Builder()
                .index("system-info")
                .sort(timestamp)
                .size(1)
                .build();
        SearchResponse<SystemInfoVo> search = client.search(searchRequest, SystemInfoVo.class);
        SystemInfoVo systemInfoVo = search.hits().hits().stream().map(s -> s.source()).findFirst().orElse(null);
        System.out.println(systemInfoVo);

    }
}
